package reflection;

import reflection.entity.Graduate;
import reflection.service.MyInterface;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author liweimin
 * @date 2021/4/13 16:00
 * @description 动态代理
 * @since
 */
public class DynamicProxy implements InvocationHandler
{

    private Object object;

    public DynamicProxy(Object object) {
        this.object = object;
    }

    public static void main(String[] args) {
        MyInterface myInterface = new Graduate();
        DynamicProxy dynamicProxy = new DynamicProxy(myInterface);
        MyInterface instance = (MyInterface) Proxy.newProxyInstance(Graduate.class.getClassLoader(), Graduate.class.getInterfaces(), dynamicProxy);

//        System.out.println(instance.getClass().getSuperclass());

        instance.saySomething("213");
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("==================动态代理开始=================");
        Object returnValue = method.invoke(object, args);
        System.out.println(proxy.getClass());
        System.out.println(proxy.getClass().getName());

        System.out.println(this.getClass().getName());
        System.out.println("==================动态代理结束=================");
        return returnValue;
    }
}
